#include <iostream>
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>

char* toHex(pthread_t tid){
    char *buffer = (char*)malloc(32);
    snprintf(buffer, 32, "0x%lX", tid);
    return buffer;
}


void *thread1_run(void *args)
{
    pthread_t tid = pthread_self();
    while (true)
    {
        char* tmp = toHex(tid);
        
        std::cout << "thread1 running pid: " << getpid() << tmp << std::endl;
        sleep(1);
        free(tmp);
    }
    return nullptr;
}

int main()
{
    std::cout << "进程 pid:" << getpid() << std::endl;
    pthread_t tid;
    pthread_create(&tid, nullptr, thread1_run, (void *)"thread1");
    char* tmp = toHex(tid);
    std::cout << "main thread————thread1 tid: " <<  tmp << std::endl;
    free(tmp);
    while(true){
        std::cout << "main thread pid: " << getpid() << std::endl;
        sleep(1);
    }

    return 0;
}